function TermPicker(termInput, start, end) {
    function addMonths(dateStr, months) {
	var timepart = dateStr.split('-');
	var flag = 0;

	var month = parseInt(timepart[1]) + months;
	if (month > 12) {
	    flag = parseInt(month / 12);
	    month = month % 12;
	}
	var year = parseInt(timepart[0]) + flag;

	return (year + '-' + month + '-' + timepart[2]);
    }

    function syn() {
	var num = parseInt(termInput.val());
	if (num) {
	    var beginStr = start.val();
	    var endStr = addMonths(beginStr, num);
	    end.val(endStr);
	}
    }

    // 初始化数据
    if (!end.val()) {
	syn();
    } else {
	var endStr = end.val();
	var startStr = start.val();
	var startTime = startStr.split('-');
	var endTime = endStr.split('-');

	var year = parseInt(endTime[0]) - parseInt(startTime[0]);
	var months = year * 12;

	var month = parseInt(endTime[1]) - parseInt(startTime[1]);
	months = months + month;

	termInput.val(months);
    }

    // 只能输入数字
    termInput.keydown(function(e) {
	var code = e.which;
	if (!e.shiftKey && (code == 8 || (code > 47 && code < 58))) {
	    return true;
	} else {
	    return false;
	}
    });

    // 改变期限改变结束时间
    termInput.change(function() {
	syn();
    });

    // 改变开始时间改变结束时间
    start.change(function() {
	syn();
    });

    // 支持下拉菜单选择时间
    termInput.closest("div").find("li a").click(function() {
	var num = $(this).html();
	termInput.val(num);
	syn();
    });
}
